package com.handydata.antifraud.service.impl;

import com.handydata.antifraud.bo.citygpsbasedata.CityGpsBasedataBeanBO;
import com.handydata.antifraud.bo.citygpsbasedata.CityGpsBasedataReqBO;
import com.handydata.antifraud.bo.citygpsbasedata.CityGpsBasedataResBO;
import com.handydata.antifraud.common.exception.CityGpsBasedataExCode;
import com.handydata.antifraud.dao.CityGpsBasedataDao;
import com.handydata.antifraud.entity.CityGpsBasedataDO;
import com.handydata.antifraud.service.CityGpsBasedataService;
import com.handydata.common.convert.CommonBeanConvertor;
import com.handydata.common.exception.SysException;
import com.handydata.common.service.AbsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("cityGpsBasedataService")
public class CityGpsBasedataServiceImpl extends AbsService implements CityGpsBasedataService {
	
	private static final Logger logger = LoggerFactory.getLogger(CityGpsBasedataServiceImpl.class);
	
	@Autowired
	private CityGpsBasedataDao cityGpsBasedataDao;

	@Transactional
	@Override
	public CityGpsBasedataResBO createCityGpsBasedata(CityGpsBasedataReqBO req) throws SysException{
	    CityGpsBasedataResBO res = new CityGpsBasedataResBO();
		try {
			CityGpsBasedataDO entity = CommonBeanConvertor.convert(req, CityGpsBasedataDO.class);
			long status = cityGpsBasedataDao.insert(entity);
			if(status == 0) {
				res.setExCode(CityGpsBasedataExCode.SYS_NO_ENTITY_CREATED);
			} else {
				res.setExCode(CityGpsBasedataExCode.SUCCESS);
			}
 			
		} catch (Exception e) {
			logger.error("Create new entity {} get into error", req, e);
			throw new SysException(CityGpsBasedataExCode.SYS_CREATE_ENTITY_ERROR);
		}
		
		return res;
	}

	@Transactional
	@Override
	public CityGpsBasedataResBO modifyCityGpsBasedata(CityGpsBasedataReqBO req) throws SysException{
	    CityGpsBasedataResBO res = new CityGpsBasedataResBO();
		try {
			CityGpsBasedataDO condition = CommonBeanConvertor.convert(req, CityGpsBasedataDO.class);
			long status = cityGpsBasedataDao.update(condition);
			if(status == 0) {
				res.setExCode(CityGpsBasedataExCode.SYS_NO_ENTITY_MODIFIED);
			} else {
				res.setExCode(CityGpsBasedataExCode.SUCCESS);
			}
		} catch (Exception e) {
			logger.error("Modify entity {} get into error", req, e);
			throw new SysException(CityGpsBasedataExCode.SYS_MODIFY_ENTITY_ERROR);
		} 
		return res;
	}

	@Transactional
	@Override
	public CityGpsBasedataResBO deleteCityGpsBasedata(CityGpsBasedataReqBO req) throws SysException{
	    CityGpsBasedataResBO res = new CityGpsBasedataResBO();
		
		try {
			CityGpsBasedataDO condition = CommonBeanConvertor.convert(req, CityGpsBasedataDO.class);
			long status = cityGpsBasedataDao.delete(condition);
			if(status == 0) {
				res.setExCode(CityGpsBasedataExCode.SYS_NO_ENTITY_DELETED);
			} else {
				res.setExCode(CityGpsBasedataExCode.SUCCESS);
			}
		} catch (Exception e) {
			logger.error("Delete entity {} get into error", req, e);
			throw new SysException(CityGpsBasedataExCode.SYS_DELETE_ENTITY_ERROR);
		}
		return res;
	}

	@Override
	public CityGpsBasedataResBO queryCityGpsBasedata(CityGpsBasedataReqBO req) throws SysException{
		CityGpsBasedataDO condition = CommonBeanConvertor.convert(req, CityGpsBasedataDO.class);
		CityGpsBasedataResBO res;
		
		try {
			List<CityGpsBasedataDO> entities = cityGpsBasedataDao.query(condition);
			res = CommonBeanConvertor.convert(entities, CityGpsBasedataResBO.class, CityGpsBasedataBeanBO.class);
		} catch(Exception e) {
			logger.error("Query entity {} get into error", req, e);
			throw new SysException(CityGpsBasedataExCode.SYS_QUERY_ENTITY_ERROR);
		}
		return res;
	}
	
}
